package pers.chao.tool.validate;

import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Date;
import java.util.Map;

/**
 * Description: 判断对象、字符串、集合是否为空、不为空
 *
 * @author W.Chao
 * @date 2020/11/14 18:54
 **/
public final class ObjValidator {
	private ObjValidator(){}
	/**
	 * 判断数组是否为空
	 *
	 * @param array
	 * @return boolean
	 */
	@SuppressWarnings("unused")
	private static <T> boolean isEmptyArray(T[] array) {
		return array == null || array.length == 0;
	}

	/**
	 * 判断数组是否不为空
	 *
	 * @param array
	 * @return boolean
	 */
	public static <T> boolean isNotEmptyArray(T[] array) {
		return array != null && array.length > 0;
	}

	/**
	 * 判断字符串是否为空
	 *
	 * @param string
	 * @return boolean
	 */
	public static boolean isEmptyString(String string) {
		return string == null || string.length() == 0;
	}

	/**
	 * 判断字符串是否不为空
	 *
	 * @param string
	 * @return boolean
	 */
	public static boolean isNotEmptyString(String string) {
		return string != null && string.length() > 0;
	}

	/**
	 * 判断集合是否为空
	 *
	 * @param collection
	 * @return boolean
	 */
	public static boolean isEmptyCollection(Collection<?> collection) {
		return collection == null || collection.isEmpty();
	}

	/**
	 * 判断集合是否不为空
	 *
	 * @param collection
	 * @return boolean
	 */
	public static boolean isNotEmptyCollection(Collection<?> collection) {
		return collection != null && !collection.isEmpty();
	}

	/**
	 * 判断map集合是否不为空
	 *
	 * @param map
	 * @return boolean
	 */
	@SuppressWarnings("rawtypes")
	public static boolean isNotEmptyMap(Map map) {
		return map != null && !map.isEmpty();
	}

	/**
	 * 判断map集合是否为空
	 *
	 * @param map
	 * @return boolean
	 */
	@SuppressWarnings("rawtypes")
	public static boolean isEmptyMap(Map map) {
		return map == null || map.isEmpty();
	}

	/**
	 * 检验对象是否为空,String 中只有空格在对象中也算空.
	 *
	 * @param object
	 * @return 为空返回true, 否则false.
	 */
	@SuppressWarnings("rawtypes")
	public static boolean isEmptyObj(Object object) {
		if (null == object)
			return true;
		else if (object instanceof String)
			return "".equals(object.toString().trim());
		else if (object instanceof Iterable)
			return !((Iterable) object).iterator().hasNext();
		else if (object.getClass().isArray())
			return Array.getLength(object) == 0;
		else if (object instanceof Map)
			return ((Map) object).size() == 0;
		else if (Number.class.isAssignableFrom(object.getClass()))
			return false;
		else if (Date.class.isAssignableFrom(object.getClass()))
			return false;
		else
			return false;
	}
}
